﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BeatEmUp
{
    class GameField:DrawableGameComponent, IGameField
    {
        Texture2D line;
        Rectangle fieldRect;

        public GameField(Game game,Rectangle fieldRect)
            : base(game)
        {
            this.fieldRect = fieldRect;
        }

        public Rectangle GetFieldSize()
        {
            return fieldRect;
        }

        protected override void LoadContent()
        {
            line = new Texture2D(this.Game.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            line.SetData(new []{Color.White});
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = ((Game)this.Game).SpriteBatch;
            sb.Begin();
            DrawLine(sb, line, 3, Color.Black, new Vector2(fieldRect.X, fieldRect.Y), new Vector2(fieldRect.Width, fieldRect.Y));
            DrawLine(sb, line, 3, Color.Black, new Vector2(0, fieldRect.Height+fieldRect.Y),
                new Vector2(fieldRect.Width, fieldRect.Height+fieldRect.Y));
            sb.End();
            base.Draw(gameTime);
        }

        void DrawLine(SpriteBatch batch, Texture2D blank,
              float width, Color color, Vector2 point1, Vector2 point2)
        {
            float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
            float length = Vector2.Distance(point1, point2);

            batch.Draw(blank, point1, null, color,
                       angle, Vector2.Zero, new Vector2(length, width),
                       SpriteEffects.None, 0);
        }
    }
}
